package com.cskaoyan.service.impl;

import com.cskaoyan.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Service
public class MailServiceImpl implements MailService {
    @Autowired
    JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from; //发验证码源头邮箱
    @Value("${spring.mail.password}")
    private String password; //发验证码源头邮箱
    public boolean send_mail(String to, String title, String text) {
        //设置发件人
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            //设置收件人
            message.setTo(to);
            //设置主题
            message.setSubject(title);
//            message.setContent(text, "text/html;charset=UTF-8");
            message.setText(text);
            mailSender.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
